/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.utils;

import com.mxrck.autocompleter.TextAutoCompleter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextField;


/**
 *
 * @author xmarti
 */
public class JTextCompleterXM {
    private TextAutoCompleter textCompleter;
    private List <Object> list = new ArrayList<Object> (); 
    private JTextField jText; 
    /**
     *
     * @param list
     */
    public JTextCompleterXM(JTextField jText, List list){
        this.jText=jText; 
        this.list=list;
        this.textCompleter= new TextAutoCompleter(jText);
        this.textCompleter.addItems((ArrayList<Object>)this.list);
        this.textCompleter.setMode(0); 
    }
    public JTextField getJText () {
        return this.jText; 
    }
    public Object getSelectedItem ()
    {
        try{
            for(Object o: list){
                if (o.toString().equals(this.jText.getText())){
                    return o;                 
                }            
            }
        }catch( java.lang.NullPointerException ex) {
            return null; 
        }
                         
        return null; 
    }
    public void setSelectedItem (Object newObject)
    {
    
        try{
            for(Object o: list){
            if (o.toString().equals(newObject.toString())){
                this.jText.setText(o.toString());
                break; 
            }            
        }
        }catch( java.lang.NullPointerException ex) {
                this.jText.setText("");
        }
                         
    }    
}
